/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package core.connector.email.http.oi;

import java.io.IOException;

import core.connector.ConnectionDetails;
import core.connector.email.http.gmail.HttpConnectorGmail;
import core.util.JGoogleAnalytics;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

/**
 *
 * @author Vinicius
 */
public class HttpConnectorOi7G extends HttpConnectorGmail{

    public HttpConnectorOi7G(ConnectionDetails details) {
        super(details);
        JGoogleAnalytics.trackAsynchronously("Connector HTTP - OI 7G");
    }

    @Override@SuppressWarnings("unused")
    public boolean connect() {
        System.out.println("CONECTANDO...");
        if (user.equals("") || passwd.equals(""))
        {
            throw new IllegalStateException("Cannot connect: user and password not specified.");
        } 
        PostMethod post = new PostMethod("https://login.oiinternet.com.br/signin/");
        post.addRequestHeader("Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png;q=0.5");
        post.addRequestHeader("Connection", "keep-alive");
        post.addRequestHeader("Accept-Encoding", "gzip");
        post.addRequestHeader("Referer", "https://login.oiinternet.com.br/signin?skin=authmail-oi");
        post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        post.addParameter("returnTo","https://login.oiinternet.com.br/webmail/signin");
        post.addParameter("skin","authmail-oi");
        post.addParameter("loginButton", "Entrar");
        post.addParameter("user", user);
        post.addParameter("username_id", user+"@oi.com.br");
        post.addParameter("username_pw", passwd);
        String resultado = null;

        String location = null;
        try {
            int result = client.executeMethod(post);
            Header hlocation = post.getResponseHeader("location"); 
            if(result > 300 && hlocation != null){
            	location = hlocation.getValue();
            }            
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        if(location != null && location.endsWith("webmail/signin")){
        	GetMethod get = new GetMethod(location);
        	get.addRequestHeader("Accept","text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png;q=0.5");
        	get.addRequestHeader("Connection","keep-alive");
        	get.addRequestHeader("Accept-Encoding","gzip");
        	get.addRequestHeader("Referer","https://login.oiinternet.com.br/signin?skin=authmail-oi");
        	try {        		
				int result = client.executeMethod(get);
				resultado = getResponseBodyAsString(get);
			} catch (IOException e) {
				e.printStackTrace();
			}
            if(resultado != null && !resultado.isEmpty() && resultado.contains("SAMLResponse")){
                NameValuePair domain = new NameValuePair("dominio","oi.com.br");
                return super.acsConnect(resultado,"https://login.oiinternet.com.br/webmail/signin",domain);
           }
        }
        return false;
    }

    @Override
    public String getServiceName(){
        return "a/oi.com.br";
    }
}
